/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2012/12/16
* File: EnnImage.h
* Blog: http://www.cnblogs.com/zengqh/
**/

#ifndef EnnImage_h__
#define EnnImage_h__

#include "EnnPrerequisites.h"
#include "EnnResource.h"

namespace Enn
{
	enum CompressedFormat
	{
		CF_NONE = 0,
		CF_DXT1,
		CF_DXT3,
		CF_DXT5,
		CF_ETC1,
		CF_PVRTC_RGB_2BPP,
		CF_PVRTC_RGBA_2BPP,
		CF_PVRTC_RGB_4BPP,
		CF_PVRTC_RGBA_4BPP,
	};

	/// Compressed image mip level.
	struct CompressedLevel
	{
		/// Construct empty.
		CompressedLevel() :
	data_(0),
		width_(0),
		height_(0),
		blockSize_(0),
		dataSize_(0),
		rowSize_(0),
		rows_(0)
	{
	}

	/// Decompress to RGBA. The destination buffer required is width * height * 4 bytes. Return true if successful.
	bool Decompress(unsigned char* dest);

	/// Compressed image data.
	unsigned char* data_;
	/// Compression format.
	CompressedFormat format_;
	/// Width.
	int width_;
	/// Height.
	int height_;
	/// Block size in bytes.
	unsigned blockSize_;
	/// Total data size in bytes.
	unsigned dataSize_;
	/// Row size in bytes.
	unsigned rowSize_;
	/// Number of rows.
	unsigned rows_;
	};

	class _EnnExport Image : public Resource
	{
	public:
		Image();
		~Image();

		virtual void load(bool backgroundThread = false);

		virtual void reload(void);

		virtual void unload(void);

		bool saveBMP(const String& fileName);

		bool saveTGA(const String& fileName);

		size_t getWidth() const;
		size_t getHeight() const;

		size_t GetComponents() const { return components_; }

		uchar* getData() const;

		bool IsCompressed() const { return compressedFormat_ != CF_NONE; }

		CompressedFormat GetCompressedFormat() const { return compressedFormat_; }

		unsigned GetNumCompressedLevels() const { return numCompressedLevels_; }

		Image* GetNextLevel() const;

		CompressedLevel GetCompressedLevel(unsigned index) const;

	protected:
		virtual void loadImpl(void);

		virtual void unloadImpl(void);

	protected:
		size_t						components_;
		CompressedFormat			compressedFormat_;
		size_t						numCompressedLevels_;
	};
}
#endif    /* EnnImage_h__ */